#include<iostream>
#include<assert.h>
#include"jkg.h"
using namespace std;
void SLTPopFront(SLTNode** pphead)
{
	assert(pphead);
		assert(*pphead);
		SLTNode* newhead = (*pphead)->next;
		free(*pphead);
		*pphead = newhead;
}
SLTNode* BuyListNode(SLTDataType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL)
	{
		perror(" malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* newnode = BuyListNode(x);
	newnode->next = *pphead;
	*pphead = newnode;

}

void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pphead);
	assert(pos);
	if (pos == *pphead)
	{
		SLTPushFront(pphead, x);

	}
	else
	{
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		SLTNode* newnode = BuySListNode(x);
		prev->next = newnode;
		newnode->next = pos;

	}}
	void SLTInsertAfter(SLTNode * pos, SLTDataType x)
	{
		assert(pos);
		SLTNode* newnode = BuyListNode(x);
		newnode->next = pos->next;
    pos->next = newnode;
	}
	void SLTErase(SLTNode** pphead, SLTNode* pos)
	{
		assert(pos);
		if (pos == *pphead)
		{
			SLTPopFront(pphead);
		}
		else
		{
			SLTNode* prev = *pphead;
			while (prev->next != pos)
			{
				prev = prev->next;
			}
			prev->next = pos->next;
			free(pos);

		}
	}void SLTEraseAfter(SLTNode* pos)
	{
		assert(pos);
		assert(pos->next);
		SLTNode* posNEXT = pos->next;
		pos->next = posNEXT->next;
		free(posNEXT);
		posNEXT = NULL;
}
	/*struct ListNode* middleNode(struct ListNode* head) {
		struct ListNode* fast = head, * show = head;
		while (fast && fast->next)
		{
			fast = fast->next->next;
			show = show->next;

		}
		return show;
	}*/